package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.file.service.MinioService;
import com.heima.model.media.dtos.WmMaterialDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.core.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinioService minioService;

    @Override
    public WmMaterial getByUrl(String url) {
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUrl, url);
        return this.getOne(queryWrapper);
    }

    @Override
    public String uploadPicture(MultipartFile file) {
        // 1. 上传到minio
        String url = null;
        try{
            String originalFilename = file.getOriginalFilename();
            String newFileName = UUID.randomUUID().toString()+"."+
                    originalFilename.substring(originalFilename.indexOf(".")+1);
            url = minioService.uploadImgFile("", newFileName, file.getInputStream());
        }catch(Exception e){
            log.error("上传minio异常:{}", ExceptionUtils.getStackTrace(e));
            throw new LeadException(AppHttpCodeEnum.UPLOAD_ERROR);
        }
        // 2. 保存到数据库
        WmMaterial insertMaterial = new WmMaterial();
        insertMaterial.setUserId(UserThreadLocalUtils.getUserId());
        insertMaterial.setType(0);
        insertMaterial.setUrl(url);
        insertMaterial.setIsCollection(false);
        insertMaterial.setCreatedTime(new Date());
        this.save(insertMaterial);
        return url;
    }

    @Override
    public PageResult<WmMaterial> findByPage(WmMaterialDto dto) {
        // 查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUserId, UserThreadLocalUtils.getUserId());
        queryWrapper.eq(dto.getIsCollection()!=null, WmMaterial::getIsCollection, dto.getIsCollection());
        // 分页参数
        Page<WmMaterial> ipage = new Page<>(dto.getPage(), dto.getSize());
        Page<WmMaterial> page = this.page(ipage, queryWrapper);
        return new PageResult<WmMaterial>(dto, page.getTotal(), page.getRecords());
    }
}
